package processor;

import connector.*;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class ServletProcessor {

  URLClassLoader getServletLoader() throws MalformedURLException {
    File webroot = new File(ConnectorUtils.WEB_ROOT);
    URL webrootUrl = webroot.toURI().toURL();
    System.out.println("1111111111111111");
    System.out.println(webrootUrl);
    return new URLClassLoader(new URL[]{webrootUrl});
  }

  Servlet getServlet(URLClassLoader loader, Request request) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    /*
     /servlet/TimeServlet
    */
    String uri = request.getRequestURI();
    String servletName = uri.substring(uri.lastIndexOf("/") + 1);
    System.out.println(uri);
    System.out.println(servletName);

    // 需要包含包名 servlet.TimeServlet
    Class servletClass = loader.loadClass("servlet." + servletName);
    Servlet servlet = (Servlet) servletClass.newInstance();
    return servlet;
  }

  public void process(Request request, Response response) throws IOException {
    URLClassLoader loader = getServletLoader();
    try {
      // 先发送 HTTP 响应头
      response.sendHeaders();

      Servlet servlet = getServlet(loader, request);
      RequestFacade requestFacade = new RequestFacade(request);
      ResponseFacade responseFacade = new ResponseFacade(response);
      servlet.service(requestFacade, responseFacade);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (ServletException e) {
      e.printStackTrace();
    }
  }
}